import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/base/base_state.dart';
import 'package:flutter_module/base/base_stateful_widget.dart';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';

class DataSaveTest extends BaseStatefulWidget {
  @override
  _DataSaveTestState createState() => _DataSaveTestState();
}

class _DataSaveTestState extends BaseState<DataSaveTest> {
  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("数据存储"),
      ),
      body: Container(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                saveData();
              },
              child: Text("保存数据"),
            ),
            ElevatedButton(
              onPressed: () {
                getData();
              },
              child: Text("读取数据"),
            )
          ],
        ),
      ),
    );
  }

  var logger = Logger();

  void saveData() async {
    SharedPreferences sharedPreferences = await _prefs;
    var result = sharedPreferences.setString("key", "好人啊");
    result.then((value) => logger.e(value));
  }

  void getData() async {
    SharedPreferences sharedPreferences = await _prefs;
    var result = sharedPreferences.getString("key");
    logger.e(result);
    showToast(result!);
  }
}
